package com.example.iotest.bio;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

@Slf4j
public class MyServer {
    private static final byte[] buffer=new byte[1024];

    public static void main(String[] args) throws IOException {
        serve();
    }

    public static void serve() throws IOException {
        ServerSocket serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress(9001));
        log.info("Server started!!");
        for(;;){
            Socket accept = serverSocket.accept();
            InetAddress inetAddress = accept.getInetAddress();
            log.info(inetAddress.toString()+" connected!");
            InputStream inputStream = accept.getInputStream();
            int read = inputStream.read(buffer);
            log.info(inetAddress.toString()+": "+new String(buffer,0,read));
            inputStream.close();
            log.info(inetAddress.toString()+" disconnected!");
        }
    }
}
